﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

namespace App26
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.PointerMoved += (s, e) =>
            {
                if (PauseButton.IsChecked.Value)
                    return;
                var point = e.GetCurrentPoint(this);
                if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
                    if (!point.Properties.IsLeftButtonPressed)
                        return;
                for (int i = 0; i < AmountSlider.Value; i++)
                    Splarkle(point);
            };
        }

        Random _Random = new Random((int)DateTime.Now.Ticks);
        private void Splarkle(Windows.UI.Input.PointerPoint point)
        {
            var x = point.Position.X;
            var y = point.Position.Y;
            var s = .5d;
            var dot = new Windows.UI.Xaml.Shapes.Ellipse
            {
                Height = 10,
                Width = 10,
                Opacity = 1,
                VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top,
                HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left,
                Fill = new SolidColorBrush(GetRandColor()),
                RenderTransform = new CompositeTransform
                {
                    TranslateX = x,
                    TranslateY = y,
                    ScaleX = s,
                    ScaleY = s,
                },
            };
            LayoutRoot.Children.Add(dot);

            var d = TimeSpan.FromMilliseconds(_Random.Next(300, 1200));

            x += _Random.Next(-100, 100);
            var atx = new DoubleAnimation { To = x, Duration = d };
            Storyboard.SetTarget(atx, dot.RenderTransform);
            Storyboard.SetTargetProperty(atx, "TranslateX");

            y += _Random.Next(-50, 100);
            var aty = new DoubleAnimation { To = y, Duration = d };
            Storyboard.SetTarget(aty, dot.RenderTransform);
            Storyboard.SetTargetProperty(aty, "TranslateY");

            s += _Random.Next(1, 2);
            var asx = new DoubleAnimation { To = s, Duration = d };
            Storyboard.SetTarget(asx, dot.RenderTransform);
            Storyboard.SetTargetProperty(asx, "ScaleX");

            var asy = new DoubleAnimation { To = s, Duration = d };
            Storyboard.SetTarget(asy, dot.RenderTransform);
            Storyboard.SetTargetProperty(asy, "ScaleY");

            var ao = new DoubleAnimation { To = 0, Duration = d };
            Storyboard.SetTarget(ao, dot);
            Storyboard.SetTargetProperty(ao, "Opacity");

            var story = new Storyboard();
            story.Completed += (sender, e) => LayoutRoot.Children.Remove(dot);
            story.Children.Add(atx);
            story.Children.Add(aty);
            story.Children.Add(asx);
            story.Children.Add(asy);
            story.Children.Add(ao);
            story.Begin();
        }

        public Color GetRandColor()
        {
            return new Color()
            {
                B = (byte)_Random.Next(255),
                G = (byte)_Random.Next(255),
                R = (byte)_Random.Next(255),
                A = 255
            };
        }
    }
}
